import { getToken, setToken } from '@/utils/auth'
import { login, smsLogin } from '@/api/user'
import  { default as localStorage }  from '@/utils/localStorage'
const app = {
  state: {
    token: getToken()
  },
  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    }
  },
  actions: {
    login ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        login(userInfo).then(response => {
          const data = response.data;
          setToken(data.token);
          localStorage.setItem('userMobile',data.mobile);
          commit('SET_TOKEN', data.token);
          resolve()
        }).catch(error => {
          console.log('登录错误：' + error);
          reject(error)
        })
      })
    },
    smsLogin ({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        smsLogin(userInfo).then(response=> {
          const data = response.data
          setToken(data.token)
          commit('SET_TOKEN', data.token)
          resolve()
        }).catch(error => {
          console.log('登录错误：' + error)
          reject(error)
        })
      })
    }
  }
}
export default app
